from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QMessageBox
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer

# 导入窗体内容
from design.mainWindow import *

from common.logger import logger
from common.retcode import RetCode
from common.utils import is_ftype_image, is_ftype_video, center_window_on_screen
from api.user import UserAction

from windows.personal_info import *
from windows.sign import *
from windows.setting import *

from windows.main_files_table import *
from windows.main_share_table import *
from windows.main_recycle_table import *
from windows.main_transfer_table import *

# 创建 MainWin 类并传入Ui_MainWindow
class MainWin(QMainWindow, Ui_MainWindow):
	def __init__(self, login_win, parent=None):
		super(MainWin, self).__init__(parent)
		self.setupUi(self)
		center_window_on_screen(self)

		self._is_init = False
		self._sess_wp = login_win._sess_wp
		self._login_win = login_win

		self._personal_info_dlg = None
		self._sign_dlg = None
		self._setting_dlg = None

		self.actionPersonalInfo.triggered.connect(self.onClickedPersonalInfo)
		self.actionShiftAccount.triggered.connect(self.onClickedShiftAccount)
		self.actionQuit.triggered.connect(self.onClickedQuit)
		self.actionSign.triggered.connect(self.onClickedSign)
		self.actionSetting.triggered.connect(self.onClickedSetting)
		self.showEvent = self.onShowWindow
		self.closeEvent = self.onCloseWindow

		self.progressBarVolumeUsed.setStyleSheet('''
			QProgressBar {
				min-height: 15px;
				max-height: 15px;
				border-radius: 6px;
			}
			QProgressBar::chunk {
				border-radius: 6px;
				background-color: lightgreen;
			}
		'''
		)

		self._files_table = MainFilesTable(self)
		self._share_table = MainShareTable(self)
		self._recycle_table = MainRecycleTable(self)
		self._transfer_table = MainTransferTable(self)

	########################################################################

	def onShowWindow(self, event):
		if self._is_init:
			return

		self.labelLoginStatus.setText('正在获取用户信息...')
		
		self.progressBarVolumeUsed.setValue(0)
		self._sess_wp.set_user_info(None)
		
		self._files_table.onShowWindow()
		self._share_table.onShowWindow()
		self._recycle_table.onShowWindow()
		
		QTimer.singleShot(0, self.initUserInfo)

	def onCloseWindow(self, event):
		self._transfer_table.onShutDown()
		self._sess_wp.save_config()

	########################################################################

	def initUserInfo(self):
		self.getUserInfo()
		self.labelLoginStatus.setText('正在获取根目录信息...')
		QTimer.singleShot(0, self.initRootFolder)

	def getUserInfo(self):
		user_action = UserAction(self._sess_wp)
		code, user_info = user_action.get_user_infos()
		if code != RetCode.SUCCESS:
			self.labelLoginStatus.setText(f'获取用户信息失败: {code}')
			return
		else:
			logger.debug(f'user_info: {user_info}')
			self.labelLoginStatus.setText('获取用户信息成功')
			self._sess_wp.set_user_info(user_info)

			# print(user_info.used, user_info.quota)

			usedGb = round(user_info.used / (1<<30), 2)
			quotaGb = round(user_info.quota / (1<<30), 2)
			pcnt = round(user_info.used * 100 / user_info.quota, 2)
			self.progressBarVolumeUsed.setValue(int(pcnt))
			self.progressBarVolumeUsed.setFormat(f'{usedGb}G / {quotaGb}G')

	def initRootFolder(self):
		root_fid = -11
		self._files_table.getFolder(root_fid, True)

		self.labelLoginStatus.setText('正在获取分享信息...')
		QTimer.singleShot(0, self.initShareList)

	def initShareList(self):
		self._share_table.getShareList(1)

		self.labelLoginStatus.setText('正在获取回收站信息...')
		QTimer.singleShot(0, self.initRecycleList)

	def initRecycleList(self):
		self._recycle_table.getRecycleList()

		self.initTransferList()

		user_info = self._sess_wp.get_user_info()
		self.labelLoginStatus.setText(f'状态: 已登录[{user_info.account}]')
		self._is_init = True

	def initTransferList(self):
		transfer_list = self._sess_wp.get_transfer_list()
		if transfer_list is not None:
			self._transfer_table.refresh_transfer_list(transfer_list)

	########################################################################

	def getFileIconLabel(self, file_info):
		fileIconLabel = QLabel()
		if file_info.isFolder:
			fileIconLabel.setPixmap(QPixmap(':/icon/icons/folder.png'))
		elif is_ftype_image(file_info.ftype):
			fileIconLabel.setPixmap(QPixmap(':/icon/icons/image.png'))
		elif is_ftype_video(file_info.ftype):
			fileIconLabel.setPixmap(QPixmap(':/icon/icons/video.png'))
		else:
			fileIconLabel.setPixmap(QPixmap(':/icon/icons/text.png'))

		return fileIconLabel

	########################################################################

	def onClickedPersonalInfo(self):
		if not self._sess_wp:
			QMessageBox.information(self, "提示", "用户尚未登录")
			return
		user_info = self._sess_wp.get_user_info()
		if not user_info:
			QMessageBox.information(self, "提示", "尚未能获取到用户信息")
			return

		if not self._personal_info_dlg:
			self._personal_info_dlg = PersonalInfoDlg()
		self._personal_info_dlg.setUserInfo(user_info)
		self._personal_info_dlg.show()

	def onClickedShiftAccount(self):
		self._is_init = False
		self._transfer_table.stopWorking()
		self.hide()
		self._sess_wp.save_config()
		self._login_win.reset()
		self._login_win.show()

	def onClickedQuit(self):
		self._transfer_table.onShutDown()
		self._sess_wp.save_config()
		QApplication.instance().quit()

	def onClickedSign(self):
		if not self._sess_wp:
			QMessageBox.information(self, "提示", "用户尚未登录")
			return
		if not self._sign_dlg:
			self._sign_dlg = SignDlg()
		self._sign_dlg.reset(self._sess_wp, self)
		self._sign_dlg.show()

	def onClickedSetting(self):
		if not self._setting_dlg:
			self._setting_dlg = SettingDlg()
		self._setting_dlg.reset(self._sess_wp)
		self._setting_dlg.show()
